﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_Default2 : System.Web.UI.Page
{
    FoodType ft;
    protected void Page_Load(object sender, EventArgs e)
    {

        DBconnect connect = new DBconnect();
        connect.Open();

        btSualoaimon.Visible = false;
        //Xóa loại món
        if (Page.Request.QueryString["del"] != null)
        {
            Xoaloaimon(Page.Request.QueryString["del"]);
        }
        //Sua loại món
        if (Page.Request.QueryString["edit"] != null)
        {
            //int id = Int32.Parse(Page.Request.QueryString["edit"]);
            //DataTable DBTable1 = new DataTable();
            //DBTable1 = connect.GetDataTable("Select * from FoodType Where FoodTypeId=@id", "@id", id);
            //txtFoodName.Text = DBTable1.Rows[0]["Name"].ToString();
            btSualoaimon.Visible = true;
            btThemmon.Visible = false;
            btSualoaimon.Click += new EventHandler(btSualoaimon_Click);
        }

        //Hiển thị danh sách loại món ăn

        if (connect.IsConnected == true)
        {
            string myQuery = "Select * from FoodType";
            DataTable DBTable = new DataTable();
            DBTable = connect.GetDataTable(myQuery);

            string output = "<tr height=15pt bgcolor=#c0c0c0><td align=center>STT</td><td align=center>Mã nhóm SP</td><td align=center>Tên nhóm SP</td><td align=center>Chức năng</td></tr>";
            for (int i = 0; i < DBTable.Rows.Count; i++)
            {
                output += "<tr height=15pt>";
                output += "<td>" + i + "</td>";
                output += "<td>" + DBTable.Rows[i]["FoodTypeId"].ToString() + "</td>";
                output += "<td>" + DBTable.Rows[i]["Name"] + "</td>";
                output += "<td align=center>";
                output += "<font face=\"verdana\" size=\"2\"><a onclick=\"return confirm('Bạn có chắc chắn muốn xóa " + DBTable.Rows[i]["Name"] + " không?')\" onmouseover=\"window.status='&nbsp;Xóa nhóm sản phẩm " + DBTable.Rows[i]["Name"] + " ';return true\" onmouseout=\"window.status='Chào mừng ! ';return true\" href=QLLoaimon.aspx?del=" + DBTable.Rows[i]["FoodTypeId"] + "><strong><span style=\"text-decoration: none; color: #Ff6633\">[Xóa]</span></strong></a></font>&nbsp;";
                output += "<font face=\"verdana\" size=\"2\"><a onclick=\"return confirm('Bạn có chắc chắn muốn sửa " + DBTable.Rows[i]["Name"] + " không?')\" onmouseover=\"window.status='&nbsp;Sửa nhóm sản phẩm " + DBTable.Rows[i]["Name"] + " ';return true\" onmouseout=\"window.status='Chào mừng ! ';return true\" href=QLLoaimon.aspx?edit=" + DBTable.Rows[i]["FoodTypeId"] + "><strong><span style=\"text-decoration: none; color: #669933\">[Sửa]</span></strong></a></font>&nbsp;";
                output += "</td>";
                output += "</tr>";
            }
            connect.Close();
            this.noidungloaimon.InnerHtml = output;
            //Debug
            //if (DBTable.Rows.Count > 0)
            //    lbft_error.Text = "Successfull read data";
            //else lbft_error.Text = "Error read data";
            //End of debug
        }
        else
            lbft_error.Text = "Lỗi kết nối..";

    }

    void Xoaloaimon(String ID)
    {
        DBconnect connect = new DBconnect();
        ft = new FoodType();
        int n = ft.delete_FoodType(Int32.Parse(ID));
        if (n > 0)
            lbft_error.Text = "Deleted";
        else
            lbft_error.Text = "Couldn't Delete";
        this.Page.Response.Redirect("QLLoaimon.aspx");
    }

    void Sualoaimon(String ID, string name)
    {
        ft = new FoodType();
        int n = ft.Edit_FoodType(Int32.Parse(ID), name);
        if (n > 0)
        {
            lbft_error.Text = "Đã cập nhật";
            txtFoodName.Text = "";
        }
        else
            lbft_error.Text = "Lỗi cập nhật";
    }

    protected void btThemmon_Click(object sender, EventArgs e)
    {
        btSualoaimon.Visible = false;
        ft = new FoodType();
        ft.FoodName = txtFoodName.Text;
        int i = ft.insert_FoodType();
        if (i > 0)
        {
            lbft_error.Text = "Successfull";
            this.Page.Response.Redirect("QLLoaimon.aspx");
        }
        else
        {
            lbft_error.Text = "Error when write data" + i;
        }
    }
    protected void btSualoaimon_Click(object sender, EventArgs e)
    {
        string ID = Page.Request.QueryString["edit"];
        if (txtFoodName.Text == "")
        {
            return;
        }
        else
        {
            Sualoaimon(ID, txtFoodName.Text);
        }
        this.Page.Response.Redirect("QLLoaimon.aspx");
    }
}
